package util

import (
	"crypto/rand"
	"encoding/base64"
	"fmt"
)

func generateUniqueString(length int) (string, error) {
	buffer := make([]byte, length)
	_, err := rand.Read(buffer)
	if err != nil {
		return "", err
	}
	// 将字节切片转换为base64编码的字符串
	uniqueString := base64.RawURLEncoding.EncodeToString(buffer)
	// 检查生成的字符串长度是否超过指定的长度，如果超过则截取出指定长度的子串
	if len(uniqueString) > length {
		uniqueString = uniqueString[:length]
	}
	return uniqueString, nil
}

func GetUniqueID() string {
	length := 10
	uniqueStr, err := generateUniqueString(length)
	if err != nil {
		fmt.Println("Error:", err)
		return ""
	}
	fmt.Println("Unique String:", uniqueStr)
	return uniqueStr
}
